<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=no"/>
    <meta charset="utf-8">
    <title>Promises</title>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<h1>Promises</h1>
<p>Open the console</p>
<script type="text/babel">

    const getFakeMembers = count => new Promise((resolves, rejects) => {
      const api = `https://api.randomuser.me/?nat=US&results=${count}`
      const request = new XMLHttpRequest()
      request.open('GET', api)
      request.onload = () =>
           (request.status === 200) ?
            resolves(JSON.parse(request.response).results) :
            rejects(Error(request.statusText))
      request.onerror = (err) => rejects(err)
      request.send()
    })

    getFakeMembers(5).then(
      members => console.log(members),
      err => console.error(
          new Error("cannot load members from randomuser.me"))
    )

</script>
</body>
</html>
